﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace TemplateWeb.Controllers
{
    public class PhapLyResult: ActionResult
    {
        public string FileName { get; set; }
        public string Path { get; set; }
        public string FileExtension { get; set; }

        public override void ExecuteResult(ControllerContext context)
        {
            context.HttpContext.Response.Buffer = true;
            context.HttpContext.Response.Clear();
            context.HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" + FileName);
            string[] compFile = FileName.Split(new char[] { '.' });
            FileExtension = compFile[compFile.Length - 1];
            switch (FileExtension.Trim().ToLower())
            {
                case "xls":
                    context.HttpContext.Response.ContentType = "application/vnd.ms-excel";
                    break;
                case "doc":
                    context.HttpContext.Response.ContentType = "application/msword";
                    break;
                case "pdf":
                    context.HttpContext.Response.ContentType = "application/pdf";
                    break;
            }
            context.HttpContext.Response.WriteFile(context.HttpContext.Server.MapPath(Path));
        }
    }
}